import 'package:flutter/material.dart';
import 'package:xvideos/hj_model/home/home_tabs_entity.dart';
import 'package:xvideos/hj_model/home/module_detail_model.dart';
import 'package:xvideos/hj_page/cartoon_business/cartoon_sub_module_page.dart';
import 'package:xvideos/hj_utils/api_service/vid_service.dart';
import 'package:xvideos/tools_base/base_list_controller.dart';

class CartoonSubGridLogic extends ListBaseLogic {
  final CommunityType type; //-1:动漫和漫画 0:图集 1:黄游 2:小说
  final ModuleData? tagData; //模块model
  List sortType = [];
  List<int> sortValues = [];
  List<AllSection> specials = []; //动漫和漫画 添加标签
  late TabController tabCtr;

  int get crossAxisCount {
    if (type == CommunityType.Cartoon) return 3;
    if (type == CommunityType.Pic) return 2;
    if (type == CommunityType.Game) return 2;
    if (type == CommunityType.Novel) return 3;
    return 0;
  }

  double get childAspectRatio {
    if (type == CommunityType.Cartoon) return 111 / 190;
    if (type == CommunityType.Pic) return 168 / 246;
    if (type == CommunityType.Game) return 168 / 137;
    if (type == CommunityType.Novel) return 111 / 190;
    return 1;
  }

  String get newsType {
    if (tagData?.moduleName == '热门推荐' && tagData?.type == -100) return 'PIC';
    if (tagData?.moduleName == '热门推荐' && tagData?.type == -101) return 'SEED_LINK';
    return '';
  }

  bool get isAcg => type == CommunityType.Novel || type == CommunityType.Cartoon; //是否是acg模块

  CartoonSubGridLogic({this.tagData, this.type = CommunityType.Pic});

  @override
  onInit() {
    super.onInit();
    if (type == CommunityType.Cartoon) {
      sortType = ['热门推荐', '最新上架', '最多观看', '最多收藏']; //排序
      sortValues = [2, 1, 3, 7];
    } else if (type == CommunityType.Pic || type == CommunityType.Novel) {
      sortType = ['最多收藏', '最新上架', '最多观看']; //排序
      sortValues = [7, 1, 3];
    } else {
      sortType = ['最新', '最热', '畅销']; //排序
      sortValues = [1, 2, 8];
    }
    tabCtr = TabController(length: sortType.length, vsync: this);
  }

  @override
  onReady() {
    super.onReady();
    loadData();
  }

  //获取海角样式数据
  loadData({int pageNum = 1}) async {
    dataList ??= [];
    ModuleDetailModel? retModel;
    if (tagData?.moduleName == '热门推荐' && (tagData?.type == -100 || tagData?.type == -101)) {
      //图集
      retModel = await VidService.communityRcommend(
        pageNum,
        newsType: newsType,
        sortType: sortValues[tabCtr.index],
      );
      if (retModel != null) {
        if (pageNum == 1) dataList?.clear();
        currentPage = pageNum;
        dataList?.addAll(retModel.allVideoInfo ?? []); //设置海角样式列表
      }
    } else {
      retModel = await VidService.getModuleDetail(
        tagData?.id ?? "",
        pageNumber: pageNum,
        pageSize: 12,
        moduleSort: sortValues[tabCtr.index],
      );

      if (retModel != null) {
        currentPage = pageNum;
        if (pageNum == 1) {
          dataList?.clear();
          specials.clear();
          specials.addAll(retModel.allSection ?? []);
        }
        if (isAcg) {
          dataList?.addAll(retModel.allMediaInfo ?? []); //设置海角样式列表
        } else {
          dataList?.addAll(retModel.allVideoInfo ?? []); //设置海角样式列表
        }
      }
    }
    update();
    refreshCtr?.refreshCompleted();
    retModel?.hasNext == true ? refreshCtr?.loadComplete() : refreshCtr?.loadNoData();
  }

  loadMoreData() => loadData(pageNum: currentPage + 1);

  //切换
  onChangeIndexAction(int index) async {
    dataList = null;
    update();
    Future.delayed(Duration(milliseconds: 50)).then((value) => loadData());
  }
}
